#pragma once

#include <vector>
#include "PlayerBase.cpp"
#include "Utils.cpp"

class PlayerManager {
private:
    std::vector<std::shared_ptr<PlayerBase>> player;

public:
    PlayerManager(std::vector<std::shared_ptr<PlayerBase>> &&player) : player{std::move(player)} {
        this->player[0]->active.store(true);
    }

    void resume() {
        for (auto p : player) {
            if (p->active)
                p->resumePlay();
            else
                p->pausePlay();
        }
    }

    void pause() {
        for (auto p : player) p->pausePlay();
    }

    void switchSteam() {
        int active = 0;
        for (auto i = 0; i < player.size(); i++) {
            if (player[i]->active.exchange(false)) active = i;
        }
        active++;
        if (active >= player.size()) active = 0;
        player[active]->active.store(true);
        resume();
    }

    int activeIndex() {
        for (auto i = 0; i < player.size(); i++) {
            if (player[i]->active.load()) return i;
        }
        return -1;
    }
};